package servlet;

import com.alibaba.fastjson.JSON;
import entity.Wallet;
import entity.WalletType;
import service.WalletTypeService;
import service.impl.WalletTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付方式
 */
@WebServlet("/WalletTypeServlet")
public class WalletTypeServlet extends HttpServlet {
    ResultSet rs;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        WalletTypeService walletTypeService = new WalletTypeServiceImpl();
        Wallet wallet = new Wallet();

        List list = new ArrayList();
        String listString = "";

        String opr = req.getParameter("opr");
        if (opr.equals("walletType")){
            int uid = Integer.parseInt(req.getParameter("uid"));
            rs = walletTypeService.selectWalletType(uid);
            try{
                while (rs.next()){
                    wallet = new Wallet();

                    int wtid = rs.getInt("wtid");
                    String wtname = rs.getString("wtname");
                    int wid = rs.getInt("wid");
                    uid = rs.getInt("uid");
                    Double wamount = rs.getDouble("wamount");

                    wallet.setWtid(wtid);
                    wallet.setWtname(wtname);
                    wallet.setWid(wid);
                    wallet.setUid(uid);
                    wallet.setWamount(wamount);

                    list.add(wallet);
                }
            }catch (Exception e){
                e.printStackTrace();
            }

            listString = JSON.toJSONString(list);
            out.print(listString);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
